<?php  	
	//session_start();
	
	require_once("includes/header.php"); 
	require_once("include_dao.php");	
		
	$arr = DAOFactory::getMovieDAO()->queryAll();	
	$arrGender = DAOFactory::getGenderDAO()->queryAll();	
	$price = 0;
	$name = "";
	
	if (isset($_GET['id']))
		$id = $_GET['id'];
	else
		$id = 1;
		
	if (isset($_GET['action']))
		$action = $_GET['action'];
	else
		$action = "empty";
	
	if($action != "empty"){
		
		if (isset($_GET['price']))
			$price = $_GET['price'];
		else
			header( 'Location: rental.php?err=31');
			
		if (isset($_GET['name']))
			$name = $_GET['name'];
		else
			header( 'Location: rental.php?err=31');
	}
	
	switch($action){
	
		case "add":
			if(isset($_SESSION['RentalCart'][$id])){						
				$_SESSION['RentalCart'][$id]["quantity"]=1;
				$_SESSION['RentalCart'][$id]["id"]=$id;
				$_SESSION['RentalCart'][$id]["price"] = $price;
				$_SESSION['RentalCart'][$id]["priceTotal"] = $price;
				$_SESSION['RentalCart'][$id]["name"] = $name;	
			}
			else{
				$_SESSION['RentalCart'][$id]["quantity"]=1;
				$_SESSION['RentalCart'][$id]["id"]=$id;
				$_SESSION['RentalCart'][$id]["price"] = $price;
				$_SESSION['RentalCart'][$id]["priceTotal"] = $price;
				$_SESSION['RentalCart'][$id]["name"] = $name;	
			}
		break;
	}	
?>

<div id="mlist">
    <div id="main">        	
        
        <div class="rightside">
            <div align="center">
                <h1> Renta de Peliculas </h1>
            </div>
        	</br>
			<?php	
                if(isset($_GET["err"])){
                    if ($_GET["err"] != ''){
                        echo("<marquee>");
                        // Mensajes de error 		
                            $msn =  msn_error($_GET["err"]);
                            echo($msn);
                        // Fin de Mensajes de Error			
                        echo("</marquee>");
                    }
                }
            ?>
            <ul class="list">
            
                <div align="center">
                   <a href="RentalCart.php"> Ver Carrito </a>
                </div>
                <?php 
					if(isset($_SESSION['RentalCart'])){
						/*print_r ($_SESSION['RentalCart']);*/
						$total = 0;
						foreach ($_SESSION['RentalCart'] as $rentalcart){
								$total += $rentalcart["priceTotal"];
						}
						
						if($total != 0){
							echo "<div align='right'>";							
								echo "Total de Alquiler : $ $total &nbsp;&nbsp;&nbsp;&nbsp;";
							echo "</div>";
							echo "</br>";
						}
					}
				?>
                </br>
                <li>
                	<?php 	
						for($i=0;$i<count($arr);$i++){
							
							$nameGender = "";
							$row = $arr[$i];
							
							for($m=0;$m<count($arrGender);$m++){
								$rowGender = $arrGender[$m];
								if($row->fKGender == $rowGender->pKGender){
									$nameGender = $rowGender->name;
								}								
							}
							
							print("
								<a href=''>
									<div class='img'>
									
										<img src='".$row->picturePath."'>									
										<div class='play_label_blue'><span></span></div>  
										<div class='add_label'>
											<a href='rental.php?id=".$row->pKMovie .
														   "&name="	.$row->name.
														   "&price=".$row->priceRentals.
														   "&action=add'>add<a>
										</div>
										
									</div>
									<div class='box'>
										<div class='rate'>
											<span style='width:".$row->rate."%'></span>
										</div>
										<div class='tit'>
											<span class='floatl'>".$row->name."</span>
											<span class='playable'> Alquiler: $ ".$row->priceRentals." </span>
										</div><div class='clearl'></div>
										<div class='ano'>".$row->year."</div>
										<div class='txt'>".$row->description."</div>
										<div class='rep'>
											<b>Reparto:</b> 
											<span class='reparto'>".$row->stakeholders."</span>
								
										</div>
										<div class='in'>
										
											<span class='genero'>".$nameGender."</span> | 
											<span class='idioma'>".$row->languages."</span> | 
											<span class='duracion'>".$row->runtime."</span> min
										</div>
									</div>
									<div class='clearl'></div>
								</a>								
								<hr></br>");
						}

					?>
                    
                    
                </li>
            </ul>
        </div>
                
        <div class="clear"></div>
        
    </div>
</div>     

<?php	
		require_once("includes/footer.php");	
?>